package com.hsav.kcxb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.adapter.SingerGroupingAdapter;
import com.hsav.kcxb.main.MSYS;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyRankBa.AObbligStyle;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.AIconType;
import com.kcbsdk.MyTools.AIconTypeList;

import java.util.ArrayList;

import static com.hsav.kcxb.main.MSYS.mMyTools;

public class SingerListMoreActivity extends BaseActivity {
    public static final String[] SINGER_GROUPS = new String[]{ "华语男歌手", "华语女歌手", "华语乐队组合", "粤语男歌手", "粤语女歌手", "粤语乐队组合", "国外男歌手", "国外女歌手", "国外粤语乐队组合" };
    public static final String KEY_SINGER_GROUP_NAME = "com.hsav.kcxb.activity.key_singer_group_name";
    private static final String TAG = SingerListMoreActivity.class.getName();

    private ListView listViewSinger;
    private SingerGroupingAdapter adapter;

    private ArrayList<AIconTypeList> hotSingers;
    private ArrayList<AIconTypeList> allSingers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_list_more);
        initView();
        String groupName = getIntent().getStringExtra(KEY_SINGER_GROUP_NAME);
        Log.d(TAG, String.format("歌手分组名字：%s", groupName));
        fetchData(groupName);
    }

    private void fetchData(final String groupName) {
        MSYS.mMyTools.setUiEventCallBack(this, new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0){
                    if (isFail) {
                        mMyTools.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                    }
                    else {
                        if (mMyTools.isSucessIconTypeListDbs(methodType, AIconType.ICON_ARTIST_HOT)){
                            if (MSYS.mMyTools.mAIconTypeList == null) return;

                            hotSingers = new ArrayList<>();
                            for (int i = 0; i < MSYS.mMyTools.mAIconTypeList.length; i++)
                            {
                                hotSingers.add(MSYS.mMyTools.mAIconTypeList[i]);
                            }
                            Log.d(TAG, String.format("热门歌手数量：%s", hotSingers.size()));

                            //获取其他歌手列表
                            MSYS.mMyTools.fetchIconTypeList(ACloudType.TYPE_KBA, AIconType.ICON_ARTIST_NAME, groupName);
                        }
                        if (mMyTools.isSucessIconTypeListDbs(methodType, AIconType.ICON_ARTIST_NAME)){
                            allSingers = new ArrayList<>();
                            for (int i = 0; i < MSYS.mMyTools.mAIconTypeList.length; i++)
                            {
                                allSingers.add(MSYS.mMyTools.mAIconTypeList[i]);
                            }
                            Log.d(TAG, String.format("所有歌手数量：%s", allSingers.size()));

                            adapter = new SingerGroupingAdapter(SingerListMoreActivity.this, hotSingers, allSingers);
                            listViewSinger.setAdapter(adapter);
                        }
                    }
                }
                else
                {
                }
            }
        });
        //获取热门歌手列表
        MSYS.mMyTools.fetchIconTypeList(ACloudType.TYPE_KBA, AIconType.ICON_ARTIST_HOT, groupName);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        allSingers = null;
        hotSingers = null;
        System.gc();
    }

    private void initView() {
        listViewSinger = (ListView) findViewById(R.id.list_view_singer);
        listViewSinger.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (adapter == null) return;
                AIconTypeList item = (AIconTypeList)adapter.getItem(position);
                SingerWorkActivity.startWithFilterSingerName(SingerListMoreActivity.this, item.TypeName);
            }
        });
    }
}
